package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SuperHotSearchMapper;
import com.ruoyi.system.domain.SuperHotSearch;
import com.ruoyi.system.service.ISuperHotSearchService;

/**
 * 热门搜索,存储热搜关键词及热度Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-14
 */
@Service
public class SuperHotSearchServiceImpl implements ISuperHotSearchService 
{
    @Autowired
    private SuperHotSearchMapper superHotSearchMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    private static final String SEARCH_COUNT_KEY = "search:count:"; // 用户搜索次数
    private static final String HOT_CLICK_KEY = "search:hot:";     // 热门点击计数
    @Override
    public void reportSearch(String keyword) {
        if (keyword == null || keyword.trim().isEmpty()) return;

        String key = SEARCH_COUNT_KEY + keyword;
        Long count = redisTemplate.opsForValue().increment(key);

        // 设置过期时间避免长期占用内存，例如 7 天
        redisTemplate.expire(key, 7, TimeUnit.DAYS);

        if (count >= 5) {
            SuperHotSearch exist = superHotSearchMapper.selectByKeyword(keyword);
            if (exist == null) {
                // 第一次超过 5 次，插入
                SuperHotSearch newHot = new SuperHotSearch();
                newHot.setKeyword(keyword);
                newHot.setScore(count.intValue());
                newHot.setDel(0);
                superHotSearchMapper.insertSuperHotSearch(newHot);
            } else {
                // 后续搜索继续增加热度
                superHotSearchMapper.updateScoreByKeyword(keyword, 1);
            }
        }
    }


    @Override
    public void increaseHotScore(String keyword) {
        if (keyword == null || keyword.trim().isEmpty()) return;

        String key = HOT_CLICK_KEY + keyword;
        Long score = redisTemplate.opsForValue().increment(key);
        // 每累计到10次就写入数据库
        if (score >= 10) {
            superHotSearchMapper.updateScoreByKeyword(keyword, score.intValue());
            redisTemplate.opsForValue().set(key, "0"); // 清零
        }
    }



    /**
     * 查询热门搜索,存储热搜关键词及热度
     * 
     * @param id 热门搜索,存储热搜关键词及热度主键
     * @return 热门搜索,存储热搜关键词及热度
     */
    @Override
    public SuperHotSearch selectSuperHotSearchById(Long id)
    {
        return superHotSearchMapper.selectSuperHotSearchById(id);
    }

    /**
     * 查询热门搜索,存储热搜关键词及热度列表
     * 
     * @param superHotSearch 热门搜索,存储热搜关键词及热度
     * @return 热门搜索,存储热搜关键词及热度
     */
    @Override
    public List<SuperHotSearch> selectSuperHotSearchList(SuperHotSearch superHotSearch)
    {
        return superHotSearchMapper.selectSuperHotSearchList(superHotSearch);
    }

    /**
     * 新增热门搜索,存储热搜关键词及热度
     * 
     * @param superHotSearch 热门搜索,存储热搜关键词及热度
     * @return 结果
     */
    @Override
    public int insertSuperHotSearch(SuperHotSearch superHotSearch)
    {
        return superHotSearchMapper.insertSuperHotSearch(superHotSearch);
    }

    /**
     * 修改热门搜索,存储热搜关键词及热度
     * 
     * @param superHotSearch 热门搜索,存储热搜关键词及热度
     * @return 结果
     */
    @Override
    public int updateSuperHotSearch(SuperHotSearch superHotSearch)
    {
        return superHotSearchMapper.updateSuperHotSearch(superHotSearch);
    }

    /**
     * 批量删除热门搜索,存储热搜关键词及热度
     * 
     * @param ids 需要删除的热门搜索,存储热搜关键词及热度主键
     * @return 结果
     */
    @Override
    public int deleteSuperHotSearchByIds(Long[] ids)
    {
        return superHotSearchMapper.deleteSuperHotSearchByIds(ids);
    }

    /**
     * 删除热门搜索,存储热搜关键词及热度信息
     * 
     * @param id 热门搜索,存储热搜关键词及热度主键
     * @return 结果
     */
    @Override
    public int deleteSuperHotSearchById(Long id)
    {
        return superHotSearchMapper.deleteSuperHotSearchById(id);
    }
}
